/*获取版本信息：使用package_info插件*/
import 'dart:io';

import 'package:frametrim/utility/toast.dart';
import 'package:package_info/package_info.dart';
import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher.dart';


class DeviceUtils {
  static late PackageInfo packageInfo;
  static PackageInfo _packageInfo = PackageInfo(
    appName: 'Unknown',
    packageName: 'Unknown',
    version: 'Unknown',
    buildNumber: 'Unknown',
  );

/*
* appName、packageName、version、buildNumber
* */

  static Future<void> getpackageInfo() async {
    packageInfo = await PackageInfo.fromPlatform();
    _packageInfo = packageInfo;
  }

/*获取版本号*/
  static String get version {
    return "V" + _packageInfo.version;
  }

/*获取AppName*/
  static String get appname {
    return _packageInfo.appName;
  }

/*获取AppName*/
  static String get packagename {
    return packageInfo.packageName;
  }

/*获取buildNumber 版本号*/
  static String get buildnumber {
    return packageInfo.buildNumber;
  }

  ///加载缓存
 static Future<Null> loadCache() async {
    try {
      Directory tempDir = await getTemporaryDirectory();
      double value = await getTotalSizeOfFilesInDir(tempDir);
    } catch (err) {
      print(err);
    }
  }

  /// 递归方式 计算文件的大小
  static Future<double> getTotalSizeOfFilesInDir(
      final FileSystemEntity file) async {
    try {
      if (file is File) {
        int length = await file.length();
        return double.parse(length.toString());
      }
      if (file is Directory) {
        final List<FileSystemEntity> children = file.listSync();
        double total = 0;
        if (children != null)
          for (final FileSystemEntity child in children)
            total += await getTotalSizeOfFilesInDir(child);
        return total;
      }
      return 0;
    } catch (e) {
      print(e);
      return 0;
    }
  }

  static renderSize(double value) {
    if (null == value) {
      return 0;
    }
    List<String> unitArr = []
      ..add('B')
      ..add('K')
      ..add('M')
      ..add('G');
    int index = 0;
    while (value > 1024) {
      index++;
      value = value / 1024;
    }
    String size = value.toStringAsFixed(2);
    return size + unitArr[index];
  }

  /*清理缓存*/
  static Future clearCache() async {
    //此处展示加载loading
    try {
      Directory  tempDir = await getTemporaryDirectory();
      double value = await getTotalSizeOfFilesInDir(tempDir);
      print("$value");
      if (value <= 0) {
        ToastUtils.show('暂无缓存');
      } else if (value >= 0) {
        // var hide = ToastUtil.showLoadingText_iOS(context, msg: "正在清理中...");
        Future.delayed(Duration(seconds: 2), () async {
          //删除缓存目录
          await delDir(tempDir);
          await loadCache();
          // ToastUtil.showSuccess(context, msg: '清理完成');
          // hide();
        });
      }

    } catch (e) {
      print(e);
      // Fluttertoast.showToast(msg: '清除缓存失败');
    } finally {}
  }


  ///递归方式删除目录
  static Future<Null> delDir(FileSystemEntity file) async {
    try {
      if (file is Directory) {
        final List<FileSystemEntity> children = file.listSync();
        for (final FileSystemEntity child in children) {
          await delDir(child);
        }
      }
      await file.delete();
    } catch (e) {
      print(e);
    }
  }

  /// 调起拨号页
  static void launchTelURL(String phone) async {
    String url = 'tel:'+ phone;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      ToastUtils.show('拨号失败！');
    }
  }
}
